package com.wica.media;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.GregorianCalendar;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.TimeZone;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.SystemClock;
import android.telephony.NeighboringCellInfo;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.telephony.gsm.GsmCellLocation;
import android.text.format.Time;
import android.util.Log;
import android.view.KeyEvent;

public class ListenPlease extends Activity {

	protected static final String LOG_TAG = "ListenPlease";
//	private BufferedWriter outRSSI,outNeighbour;
//	private BufferedReader inbuff=null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		try{
			BufferedReader inbuff = new BufferedReader(new FileReader(new File("sdcard","userID")));
			String userid = inbuff.readLine();
		}catch (Exception e) {
			Log.e(LOG_TAG, e.getMessage()+"");		
		}
		
//		outNeighbour=null;
//		outRSSI = null;
//		try {
//			outNeighbour = new BufferedWriter(new FileWriter(new File("sdcard", "neighbouring.csv")));
//			outRSSI = new BufferedWriter(new FileWriter(new File("sdcard", "rssi.csv")));
//			outRSSI.write("epoch;rssi;"+ "\n");
//			outNeighbour.write("every second 10 times");
//			
//			// RSSI
//			TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
//			if (telephonyManager != null) {
//				telephonyManager.listen(new PhoneStateListener() {
//					@Override
//					public void onSignalStrengthChanged(int asu) {
//						GregorianCalendar time_calendar = new GregorianCalendar(TimeZone.getTimeZone(Time.getCurrentTimezone()), Locale.getDefault());
//						long epoch = time_calendar.getTime().getTime();
//
//						Log.v(LOG_TAG, "RSSI at epoch ("+epoch+"):" + asu);
//						try {
//							outRSSI.write(epoch + ";" + asu );
//						} catch (IOException e) {
//							Log.e(LOG_TAG, e.getMessage());						
//						}
//					};
//				}, PhoneStateListener.LISTEN_SIGNAL_STRENGTH);
//			}		
//
//			for (int i=1;i<=10;i++){
//				// CID, RSSI
//				if (telephonyManager != null) {
//					GsmCellLocation currentCellLocation = (GsmCellLocation) telephonyManager.getCellLocation();
//					if (currentCellLocation != null) {
//						outNeighbour.write("Current Cell is " + currentCellLocation.getCid() + " and LAC is "+currentCellLocation.getLac());
//						Log.v(LOG_TAG, "Current Cell is " + currentCellLocation.getCid() + " and LAC is "+currentCellLocation.getLac());
//					}
//					List<NeighboringCellInfo> listNeighboringCellInfo=null;
//					listNeighboringCellInfo = telephonyManager.getNeighboringCellInfo();
//					if (listNeighboringCellInfo != null) {
//						Iterator<NeighboringCellInfo> iter = listNeighboringCellInfo.iterator();
//						if (iter != null) {
//							while (iter.hasNext()) {
//								NeighboringCellInfo iterating = (NeighboringCellInfo) iter.next();
//								Log.v(LOG_TAG, "the RSSI of CID " + iterating.getCid() + " is "+ iterating.getRssi());
//								outNeighbour.write("the RSSI of CID " + iterating.getCid() + " is "+ iterating.getRssi());
//							}
//						}
//					}else{
//						Log.v(LOG_TAG, "telephonyManager.getNeighboringCellInfo() IS EMPTY ");
//						outNeighbour.write("telephonyManager.getNeighboringCellInfo() IS EMPTY ");
//					}						
//				}
//				SystemClock.sleep(1000);
//			}	
//		} catch (IOException e) {			
//			Log.e(LOG_TAG,e.getMessage());
//		}
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_MENU){
			finish();
			return false;
		}else{		
			return super.onKeyDown(keyCode, event);
		}
	}
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
//		try {
//			if (outRSSI!=null)
//				outRSSI.close();
//			if (outNeighbour!=null)
//				outNeighbour.close();
//		} catch (Exception e) {
//			Log.e(LOG_TAG,e.getMessage());
//		}
	}
}
